package top.mcwebsite.playandroid.business.search.impl.ui

import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.wrapContentWidth
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.outlined.FavoriteBorder
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.Divider
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.LocalContentColor
import androidx.compose.material3.LocalTextStyle
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.paging.LoadState
import androidx.paging.compose.collectAsLazyPagingItems
import top.mcwebsite.playandroid.business.search.impl.model.SearchResultModel
import top.mcwebsite.playandroid.business.search.impl.model.SkeletonSearchResultItem
import top.mcwebsite.playandroid.business.search.impl.vm.SearchIntent
import top.mcwebsite.playandroid.business.search.impl.vm.SearchUiState
import top.mcwebsite.playandroid.business.search.impl.vm.SearchViewModel
import top.mcwebsite.playandroid.common.list.EasyLazyListPageColumn
import top.mcwebsite.playandroid.common.uiWidget.SkeletonPage
import top.mcwebsite.playandroid.strings.Str

@Composable
fun SearchResult(
    uiState: SearchUiState,
    viewModel: SearchViewModel
) {
    val lazyPagingItems =
        uiState.searchResultPagerFlow?.collectAsLazyPagingItems() ?: return
    EasyLazyListPageColumn(
        lazyPagingItems = lazyPagingItems,
        retry = {

        },
        loading = {
            SkeletonPage {
                SkeletonSearchResultItem()
            }
        },
        loadingMoreError = {

        }
    ) { index, item ->
        SearchResultItem(
            searchResultModel = item,
            onItemClick = {
                viewModel.sendIntent(SearchIntent.SearchResultItemClickIntent(it))
            }
        )
        if (index < lazyPagingItems.itemCount - 1) {
            Divider(
                modifier = Modifier.padding(horizontal = 12.dp, vertical = 5.dp)
            )
        }
    }
}

@Composable
fun SearchResultItem(
    searchResultModel: SearchResultModel,
    onItemClick: (SearchResultModel) -> Unit
) {
    Column(
        modifier = Modifier
            .padding(horizontal = 12.dp)
            .clickable {
                onItemClick(searchResultModel)
            }
    ) {
        Row(
            verticalAlignment = Alignment.CenterVertically
        ) {

            val userInfo = if (searchResultModel.author.isNotEmpty()) {
                stringResource(id = Str.author_is, searchResultModel.author)
            } else {
                stringResource(id = Str.shared_by, searchResultModel.shareUser)
            }
            CompositionLocalProvider(
                LocalTextStyle.provides(
                    MaterialTheme.typography.labelMedium.copy(color = LocalContentColor.current.copy(alpha = 0.7f))
                )
            ) {
                if (searchResultModel.fresh) {
                    Text(
                        text = stringResource(id = Str.new_new),
                        modifier = Modifier.padding(2.dp),
                        color = MaterialTheme.colorScheme.primary
                    )
                }
                Text(text = userInfo)
                Spacer(modifier = Modifier.weight(1f))
                Text(text = searchResultModel.niceDate)
            }
        }
        Text(text = searchResultModel.title, style = MaterialTheme.typography.titleMedium)
        Row(
            verticalAlignment = Alignment.CenterVertically
        ) {
            CompositionLocalProvider(
                LocalTextStyle.provides(
                    MaterialTheme.typography.labelMedium.copy(color = LocalContentColor.current.copy(alpha = 0.7f))
                )
            ) {
                Text(
                    text = listOf(
                        searchResultModel.superChapterName,
                        searchResultModel.chapterName
                    ).joinToString(" · "),
                )
                Spacer(modifier = Modifier.weight(1f))
                IconButton(onClick = { /*TODO*/ }) {
                    Icon(
                        Icons.Outlined.FavoriteBorder,
                        contentDescription = stringResource(id = Str.collection),
                        tint = Color.Red,
                        modifier = Modifier.size(28.dp)
                    )
                }
            }
        }
    }
}